package com.taikang.common.util.struts;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

import com.taikang.common.util.StringUtils;

/**
 * Struts2 Action基类.
 * 
 * 实现绕过jsp/freemaker,直接输出文本.
 * 
 */

@SuppressWarnings( { "serial" })
public class SimpleActionSupport extends ActionSupport {

	protected final Logger logger = Logger.getLogger(getClass());

	/**
	 * 绕过Template,直接输出内容的简便函数.
	 * @throws IOException 
	 */
	protected String render(String text, String contentType) {
		//modify by fee 20091022
		//为清空内存，在使用Writer后将其destroy
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType(contentType);
		PrintWriter out ;
		try {
			out = response.getWriter();;
			out.write(text);
			out.close();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	/**
	 * 直接输出字符串.
	 */
	protected String renderText(String text) {
		logger.debug(text);
		return render(text, "text/plain;charset=UTF-8");
	}

	//added by wanghuan 2009-9-29
	/**
	 * 
	 */
	protected String renderText(String text , String charset) {
		logger.debug(text);
		return render(text, "text/plain;charset=" + charset);
	}
	//end
	
	/**
	 * 直接输出HTML.
	 */
	protected String renderHtml(String html) {
		return render(html, "text/html;charset=UTF-8");
	}

	/**
	 * 直接输出XML.
	 */
	protected String renderXML(String xml) {
		return render(xml, "text/xml;charset=UTF-8");
	}

	protected static void jsonResponse(JSONObject jsonData) throws Exception {
		// check whether it is script Tag...
		// which is called by JSON
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		boolean scriptTag = false;
		String cb = request.getParameter("callback");
		if (cb != null) {
			scriptTag = true;
			response.setContentType("text/javascript;charset=UTF-8");
		} else {
			response.setContentType("text/plain;charset=UTF-8");
		}

		PrintWriter out = response.getWriter();
		if (scriptTag) {
			out.write(cb + "(");
		}
		response.getWriter().print(jsonData);
		if (scriptTag) {
			out.write(");");
		}
	
	}

	protected static void jsonOkResponse(String msg) throws Exception {
		JSONObject o = new JSONObject();
		o.put("success", true);
		o.put("msg", msg);
		jsonResponse(o);
	}
	
	protected static void jsonErrorResponse(String msg) throws Exception {
		JSONObject o = new JSONObject();
		o.put("success", false);
		o.put("msg", msg);
		jsonResponse(o);
	}
	
	protected static void jsonExceptionResponse(String title,Exception e) throws Exception {
		jsonErrorResponse(title+":<br>"+StringUtils.defaultIfEmpty(e.getMessage(), ""));
	}
}
